package com.study.talk.module.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.module.entity.Friend;
import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.entity.GroupUserInfo;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.mapper.GroupUserInfoMapper;
import com.study.talk.module.mapper.UserInfoMapper;
import com.study.talk.module.service.GroupUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thor
 * @since 2023-12-18
 */
@Service
@Transactional
public class GroupUserInfoServiceImpl extends ServiceImpl<GroupUserInfoMapper, GroupUserInfo> implements GroupUserInfoService {

    @Resource
    private GroupUserInfoMapper groupUserInfoMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public void addMember(Integer groupId, Integer userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        GroupUserInfo groupUserInfo = new GroupUserInfo(userInfo, groupId);
        groupUserInfoMapper.insert(groupUserInfo);
    }

    @Override
    public void removeMember(Integer groupId, Integer userId) {
        groupUserInfoMapper.removeGroupUserInfo(groupId,userId);
    }

    @Override
    public void removeAllMember(Integer groupId) {
        groupUserInfoMapper.removeAll(groupId);
    }

    @Override
    public boolean queryHasMember(Integer groupId, Integer userId) {
        GroupUserInfo member = groupUserInfoMapper.queryHasMember(groupId, userId);
        return ObjectUtils.isNotNull(member);
    }

    @Override
    public void deleteMember(Integer userId, Integer groupId) {
        groupUserInfoMapper.removeGroupUserInfo(groupId,userId);
    }


}
